package com.block.datapush.form.item;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.item.util.HaoHuiTuiUtil;
import com.block.datapush.form.item.util.ZYRSAUtils;
import com.block.datapush.form.item.util.ZaiYangRSAUtils;
import com.block.datapush.form.item.util.ZaiYangUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

@Service
public class ZaiYangPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {
            String queryUrl = formInfo.getQueryUrl();
            String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toUpperCase();

            Map<String, Object> bizMap = new HashMap<>();
            bizMap.put("phone_md5", mobileMd5);

            Map<String, Object>  paramMap = ZYRSAUtils.encodeParam(JSON.toJSONString(bizMap), formInfo.getPubKey(), formInfo.getPriKey());
            paramMap.put("authCode", formInfo.getApiKey());

            log.info("【杭州载阳】撞库开始【queryUrl】{}【bizMap】{}【paramMap】{}", queryUrl,  JSON.toJSONString(bizMap),  JSON.toJSONString(paramMap));
            String response = HttpUtil.createPost(queryUrl)
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【杭州载阳】撞库结束【result】{}", response);

            JSONObject responseObject = JSON.parseObject(response);
            String code = responseObject.getString("code");
            String msg = responseObject.getString("msg");

            if (StrUtil.equals(code, "0000") ) {
                return true;
            }
            return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {
        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);


            String requestUrl = formInfo.getRequestUrl();

            Map<String, Object> bizMap = new HashMap<>();
            bizMap.put("name", memberExtInfo.getUsername());
            bizMap.put("phone", memberExtInfo.getMobile());
            bizMap.put("idno", memberExtInfo.getIdCard());
            bizMap.put("city", memberExtInfo.getProvince()+"/"+memberExtInfo.getCity());

            bizMap.put("loanUsage", "9");
            bizMap.put("loanLimit", ZaiYangUtil.getLoanAmount(memberExtInfo.getLoanAmount()));
            bizMap.put("profession", "2");
            bizMap.put("workYear", "3");
            bizMap.put("gjj", ZaiYangUtil.getGjj(memberExtInfo.getGjj()));
            bizMap.put("income", "2");
            bizMap.put("incomeWay", "2");
            bizMap.put("car", ZaiYangUtil.getCar(memberExtInfo.getCar()));
            bizMap.put("house", ZaiYangUtil.getHouse(memberExtInfo.getHouse()));
            bizMap.put("zm", ZaiYangUtil.getZhiMa(memberExtInfo.getZhimaScore()));
            bizMap.put("wld", "1");
            bizMap.put("bx", ZaiYangUtil.getBaoXian(memberExtInfo.getInsurance()));
            bizMap.put("sb", ZaiYangUtil.getSheBao(memberExtInfo.getSocialInsurance()));
            bizMap.put("creditCard", ZaiYangUtil.getYuQi(memberExtInfo.getYuqi()));

            Map<String, Object>  paramMap = ZYRSAUtils.encodeParam(JSON.toJSONString(bizMap), formInfo.getPubKey(), formInfo.getPriKey());
            paramMap.put("authCode", formInfo.getApiKey());

            log.info("【杭州载阳】导单开始【requestUrl】{}【bizMap】{}【paramMap】{}", requestUrl,  JSON.toJSONString(bizMap),  JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【杭州载阳】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            String code = resultObject.getString("code");
            String msg = resultObject.getString("msg");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(code);
            pushResultDTO.setMessage(msg);
            pushResultDTO.setParamContent(JSON.toJSONString(bizMap));

            if (StrUtil.equals(code, "0000")){
                pushResultDTO.setSuccess(true);
                pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
            }else{
                pushResultDTO.setSuccess(false);
                pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
            }
            return pushResultDTO;
    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }

}
